package com.senjoeson.socialsecure.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.util.FileDownloadUtils;
import com.senjoeson.socialsecure.R;
import com.senjoeson.socialsecure.model.ArticleListBean;
import com.senjoeson.socialsecure.ui.CommentReviewActivity;
import com.senjoeson.socialsecure.ui.WebviewActivity;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author senjoeson
 * @date 2018/9/16 22:35
 */
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {


    private List<ArticleListBean> mArticleListBeans;

    public ListAdapter(List<ArticleListBean> articleListBeans) {
        this.mArticleListBeans = articleListBeans;
    }


    public void setData(List<ArticleListBean> articleListBeans) {
        this.mArticleListBeans = articleListBeans;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        return new ViewHolder(inflate);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
         ArticleListBean articleListBean = mArticleListBeans.get(position);

        holder.mTime.setText(articleListBean.getTime());
        holder.mTitle.setText(articleListBean.getTitle());
        if (articleListBean.getTextFlag() == null) {
            holder.mWhomflag.setVisibility(View.GONE);
        } else {
            holder.mWhomflag.setText(articleListBean.getTextFlag());
            holder.mWhomflag.setVisibility(View.VISIBLE);
        }
        if (articleListBean.getArticleUrl() != null && articleListBean.getArticleUrl().contains("Download")) {
            holder.mDownload.setVisibility(View.VISIBLE);
        } else {
            holder.mDownload.setVisibility(View.GONE);
        }

        holder.mDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadFile(mArticleListBeans.get(holder.getAdapterPosition()).getArticleUrl(), mArticleListBeans.get(holder.getAdapterPosition()).getTitle());
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mArticleListBeans.get(holder.getAdapterPosition()).getArticleUrl().contains("Download")) {
                    return;
                }
                if (mArticleListBeans.get(holder.getAdapterPosition()).getUuid() != null) {
                    Intent intent = new Intent(holder.itemView.getContext(), CommentReviewActivity.class);
                    intent.putExtra("uuid", mArticleListBeans.get(holder.getAdapterPosition()).getUuid());
                    holder.itemView.getContext().startActivity(intent);
                } else {
                    Intent intent = new Intent(holder.itemView.getContext(), WebviewActivity.class);
                    String url = mArticleListBeans.get(holder.getAdapterPosition()).getArticleUrl();
                    Log.d("LOCAL_URL", url);
                    intent.putExtra("url", url);
                    intent.putExtra("title", "详情");
                    holder.itemView.getContext().startActivity(intent);
                }

            }
        });
    }

    private void downloadFile(String articleUrl, String fileName) {
        final String TAG = "DOWNLOAD_LOCK";
        String defaultSaveRootPath = FileDownloadUtils.getDefaultSaveRootPath();
        String realPath = defaultSaveRootPath + File.pathSeparator + fileName;
        Log.d(TAG, "downloadFile: " + articleUrl);
        FileDownloader.getImpl().create(articleUrl)
                .setPath(realPath)
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {

                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        Log.d(TAG, "error: " + e.getLocalizedMessage());
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {

                    }
                }).start();


    }

    @Override
    public int getItemCount() {
       // Log.d("ListAdapter", "getItemCount: "+mArticleListBeans.size());
        return mArticleListBeans != null ? mArticleListBeans.size() : 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView mTitle;
        @BindView(R.id.whomflag)
        TextView mWhomflag;
        @BindView(R.id.time)
        TextView mTime;
        @BindView(R.id.download)
        Button mDownload;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
